os.loadAPI("api/turtle_utils")

function moveStep(turnLevel, turnLevel2)
	if type(turnLevel) == "number" then
		print("asd")
		turnLevel = redSignal(turnLevel, nil)
	end
	if turnLevel2 == nil then
		turnLevel2 = redSignal(nil, nil)
	elseif type(turnLevel2) == "number" then
		turnLevel2 = redSignal(turnLevel2, nil)
	end

	if not checkTurn(turnLevel, turnLevel2) then
		return false
	end
	
	turtle_utils.forward(1, false)
	
	return true
end

function redSignal(level, color)
	return {level=level, color=color, checkSide=redSignalCheckSide}
end

function redSignalCheckSide(self, side)
	print("checking side "..side.." with level "..tostring(self.level))
	if self.level ~= nil then
		print("side level is "..tostring(redstone.getAnalogInput(side)))
		if self.level == redstone.getAnalogInput(side) then
			return true
		end
	end
	return false
end

function checkTurn(turnLevel, turnLevel2)
	print("Checking turn " .. tostring(redstone.getInput("left")))
	if turnLevel.checkSide(turnLevel, "left") and not turnLevel.checkSide(turnLevel, "right") then
		print("Turning right")
		turtle.turnRight()
	elseif turnLevel.checkSide(turnLevel, "right") and not turnLevel.checkSide(turnLevel, "left") then
		print("Turning left")
		turtle.turnLeft()
	end
	return true
end

